package com.zhuhjay.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuhjay.commonutils.JwtUtils;
import com.zhuhjay.commonutils.R;
import com.zhuhjay.eduservice.entity.EduComment;
import com.zhuhjay.eduservice.service.EduCommentService;
import com.zhuhjay.servicebase.exception.GuLiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author ZhuHJay
 * @date 2022/9/9 10:24
 */
@Api(description = "前端评论控制器")
@RestController
@RequestMapping("/eduservice/commentfront")

public class CommonFrontController {

    @Resource
    private EduCommentService commentService;


    @ApiOperation("获取评论列表")
    @GetMapping("getCommons/{cId}/{tId}/{/{current}/{limit}")
    public R getCommons(@PathVariable String cId, @PathVariable String tId,
                        @PathVariable Integer current, @PathVariable Integer limit) {
        Page<EduComment> page = new Page<>(current, limit);
        commentService.page(page, new LambdaQueryWrapper<EduComment>()
                .eq(EduComment::getCourseId, cId)
                .eq(EduComment::getTeacherId, tId)
                .eq(EduComment::getIsDeleted, false)
                .orderByDesc(EduComment::getGmtCreate));
        return R.ok()
                .data("items", page.getRecords())
                .data("current", page.getCurrent())
                .data("pages", page.getPages())
                .data("size", page.getSize())
                .data("total", page.getTotal())
                .data("hasNext", page.hasNext())
                .data("hasPrevious", page.hasPrevious());
    }

    @ApiOperation("发起评论")
    @PostMapping("addCommon")
    public R addCommon(@RequestBody EduComment comment, HttpServletRequest request) {
        commentService.save(comment);
        return R.ok();
    }

}
